package cn.xz.zysjz.fragment;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import cn.xz.zysjz.R;
import cn.xz.zysjz.activity.IndexActivity;
import cn.xz.zysjz.adapter.ActivityListAdapter;
import cn.xz.zysjz.bean.ActivityListBean;
import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.databinding.FragmentActivityBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.GetDataUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 大厅模块*/
public class ActivityFragment extends Fragment {

    private FragmentActivityBinding mBinding;

    private ActivityListAdapter mAdapter; //列表适配器

    private ArrayList<HomeListBean> ml = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_activity, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //点击事件
        initEvent();
        //列表设置
        initRV();
        //获取数据
        initData();
    }

    private void initEvent(){
        //综合
        mBinding.llcS1.setOnClickListener(v->{
            selectTab(1);
        });
        //自费
        mBinding.llcS2.setOnClickListener(v->{
            selectTab(2);
        });
        //免费
        mBinding.llcS3.setOnClickListener(v->{
            selectTab(3);
        });
        //评分
        mBinding.llcS4.setOnClickListener(v->{
            selectTab(4);
        });
        //下拉刷新
        mBinding.slR.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                 mBinding.slR.postDelayed(new Runnable() {
                     @Override
                     public void run() {
                         initData();
                         mBinding.slR.setRefreshing(false);
                     }
                 },1000);
            }
        });

    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new LinearLayoutManager(this.getContext());
        mAdapter = new ActivityListAdapter(R.layout.item_activity_list);
        mBinding.rvList.setLayoutManager(ms);
        mBinding.rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ArrayList<HomeListBean> data = (ArrayList<HomeListBean>) adapter.getData();
                //跳转到详情页
                AppHelper.toJump(IndexActivity.class,data.get(position));
            }
        });
        mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (!CommonLib.isAdmin){
                    return false;
                }
                HomeListBean data = (HomeListBean) adapter.getData().get(position);
                ShowDialog.show1("确定删除吗?", getContext(), new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        GetDataUtils.delData(data.getNum());
                        adapter.remove(data);
                    }
                });
                return false;
            }
        });
    }

    private void initData(){
        ml.clear();
        ml = GetDataUtils.get(1);
        mAdapter.setList(ml);
    }


    private void selectTab(int i){
        int mC = Color.parseColor("#000000");
        Drawable mD = AppHelper.getDrawable(R.mipmap.icon_x);
        mBinding.tvS1.setTextColor(mC);
        mBinding.tvS2.setTextColor(mC);
        mBinding.tvS3.setTextColor(mC);
        mBinding.tvS4.setTextColor(mC);

        mBinding.ivS1.setImageDrawable(mD);
        mBinding.ivS2.setImageDrawable(mD);
        mBinding.ivS3.setImageDrawable(mD);
        mBinding.ivS4.setImageDrawable(mD);


        int mC1 = Color.parseColor("#00D4E2");
        Drawable mD1 = AppHelper.getDrawable(R.mipmap.icon_x1);

        switch (i){
            case 1://综合
                sx(1);
                mBinding.tvS1.setTextColor(mC1);
                mBinding.ivS1.setImageDrawable(mD1);
                break;
            case 2://自费
                sx(2);
                mBinding.tvS2.setTextColor(mC1);
                mBinding.ivS2.setImageDrawable(mD1);
                break;
            case 3://免费
                sx(3);
                mBinding.tvS3.setTextColor(mC1);
                mBinding.ivS3.setImageDrawable(mD1);
                break;
            case 4://评分
                sx(4);
                mBinding.tvS4.setTextColor(mC1);
                mBinding.ivS4.setImageDrawable(mD1);
                break;
            default:
                break;
        }
    }

    private void sx(int i){
        switch (i){
            case 1:
                mAdapter.setList(ml);
                break;
            case 2: //自费
                ArrayList<HomeListBean> mGList = new ArrayList<>();
                for (HomeListBean mD : ml){
                    if (mD.getFree() != null && !mD.getFree()){
                        mGList.add(mD);
                    }
                }
                mAdapter.setList(mGList);
                break;
            case 3: //免费
                ArrayList<HomeListBean> mFList = new ArrayList<>();
                for (HomeListBean mD : ml){
                    if (mD.getFree() != null && mD.getFree()){
                        mFList.add(mD);
                    }
                }
                mAdapter.setList(mFList);
                break;
            case 4: //免费
                ArrayList<HomeListBean> mPList = new ArrayList<>();
                mPList.addAll(ml);
                Collections.sort(mPList, new Comparator<HomeListBean>() {
                    @Override
                    public int compare(HomeListBean p1, HomeListBean p2) {
                        return  p2.getGet() - p1.getGet();
                    }
                });

                mAdapter.setList(mPList);
                break;
            default:
                break;
        }
    }
}
